<?php

declare (strict_types=1);

namespace App\Model;

use App\Utils\Upload\View;
use Hyperf\DbConnection\Model\Model;

/**
 * Class Task
 * @package App\Model
 * @property mixed|int id
 * @property mixed|int cooperative_id
 * @property mixed|int employer_id
 * @property mixed|string title
 * @property mixed|string cover_image
 * @property mixed|string product_name
 * @property mixed|string description
 * @property mixed|string tags
 * @property mixed|string audit_time
 * @property mixed|string price
 * @property mixed|string end_time
 * @property mixed|int count
 * @property mixed|int done_count
 * @property mixed|int remaining_count
 * @property mixed|int getter_user_info
 * @property mixed|string hint_message
 * @property mixed|int user_finish_time
 * @property mixed|int estimated_time
 * @property mixed|string contact
 * @property mixed|float total_price
 * @property mixed|float commission_percent
 * @property mixed|int finish_status
 * @property mixed|int show_status
 * @property mixed|int audit_status
 * @property mixed|int disable_status
 * @property mixed|int auto_payment
 * @property mixed|string created_at
 * @property mixed|string updated_at
 * @property mixed|string deleted_at
 * @property string message
 * @property int is_audit_by
 *
 * @property Cooperative cooperative
 * @property UserEmployer employer
 * @property User user
 * @property TaskStep taskSteps
 * @property TaskExec taskExec
 */
class Task extends Model
{
    protected $table = 'task';

    protected $fillable = [
        'title',
        'product_name',
        'description',
        'audit_time',
        'price',
        'count',
        'getter_user_info',
        'hint_message',
        'user_finish_time',
        'estimated_time',
        'contact'
    ];


    public function taskExec()
    {
        return $this->hasMany(TaskExec::class, 'task_id', 'id');
    }

    public function employer()
    {
        return $this->belongsTo(UserEmployer::class, 'employer_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'employer_id', 'id');
    }

    public function cooperative()
    {
        return $this->belongsTo(Cooperative::class, 'cooperative_id', 'id');
    }

    public function taskSteps()
    {
        return $this->hasMany(TaskStep::class);
    }


    public function getCoverImageAttribute($value)
    {
        if ($value) {
            return [
                'image_id' => $value,
                'image_path' => di(View::class)->fullPath($value)
            ];
        } else {
            return '';
        }
    }

    public function getTagsAttribute($value)
    {
        if ($value) {
            return json_decode($value, true);
        } else {
            return [];
        }
    }
}